// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "merchant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所属商户",
    "label": "所属商户"
  },
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所属用户",
    "label": "所属用户"
  },
  "wallet_type": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "冻结金额"
          },
          {
            "value": 1,
            "text": "可用金额"
          }
        ]
      }
    ],
    "title": "钱包类型",
    "defaultValue": 0,
    "label": "钱包类型"
  },
  "trade_type": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "收入"
          },
          {
            "value": 1,
            "text": "提现"
          },
          {
            "value": 2,
            "text": "扣款"
          },
          {
            "value": 3,
            "text": "充值"
          },
          {
            "value": 4,
            "text": "划转"
          }
        ]
      }
    ],
    "title": "交易类型",
    "defaultValue": 0,
    "label": "交易类型"
  },
  "trade_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "流水单号",
    "label": "流水单号"
  },
  "change_before_amount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "变动前金额",
    "label": "变动前金额"
  },
  "changet_after_amount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "变动后金额",
    "label": "变动后金额"
  },
  "commission_percentage": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "平台抽成",
    "label": "平台抽成"
  },
  "change_amount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "变动金额",
    "label": "变动金额"
  },
  "real_get_amount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "实际获得",
    "label": "实际获得"
  },
  "commission_amount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "抽成金额",
    "label": "抽成金额"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  }
}

const enumConverter = {
  "wallet_type_valuetotext": {
    "0": "冻结金额",
    "1": "可用金额"
  },
  "trade_type_valuetotext": {
    "0": "收入",
    "1": "提现",
    "2": "扣款",
    "3": "充值",
    "4": "划转"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
